package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a bar chart using
 * an {@link XYPlot}.
 */
public class Chart extends ApplicationFrame {

    /**
     * Constructs the demo application.
     *
     * @param title  the frame title.
     */
    public Chart(String title, int[] arr) {

        super(title);
        IntervalXYDataset intervalxydataset = createDataset(arr);   
        JFreeChart jfreechart = createChart(intervalxydataset);   
        ChartPanel chartpanel = new ChartPanel(jfreechart);   
        chartpanel.setPreferredSize(new Dimension(500, 300));   
        setContentPane(chartpanel);   
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    }
    
    private static JFreeChart createChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYBarChart(
            "Decimal Distribution",
            "Days",
            true,
            "Number Occurred",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            false,
            false
        );
        chart.setBackgroundPaint(Color.white);
        
        XYPlot plot = chart.getXYPlot();
        XYItemRenderer renderer = plot.getRenderer();
        StandardXYToolTipGenerator generator = new StandardXYToolTipGenerator(
            "{1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0"));
        renderer.setToolTipGenerator(generator);
        
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        axis.setLowerMargin(0.01);
        axis.setUpperMargin(0.01);
        return chart;
    }
    
    /**
     * Creates a sample dataset.
     */
    private static IntervalXYDataset createDataset(int[] list) {

        TimeSeries timeseries = new TimeSeries("Num Of Borrows", "Year", "dsfds", Year.class);
        try {
            timeseries.add(new Year(3), list[0]);   
            timeseries.add(new Year(6), list[1]);   
            timeseries.add(new Year(9), list[2]);   
            timeseries.add(new Year(12), list[3]);  
            timeseries.add(new Year(15), list[4]);   
            timeseries.add(new Year(18), list[5]);  
            timeseries.add(new Year(21), list[6]);   
            timeseries.add(new Year(23), list[7]);  
            timeseries.add(new Year(26), list[8]);   
            timeseries.add(new Year(30), list[9]);  

        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
        TimeSeriesCollection tsc = new TimeSeriesCollection(timeseries);
        return tsc;

    }
    
//    /**
//     * Creates a panel for the demo.
//     *  
//     * @return A panel.
//     */
//    public static JPanel createDemoPanel() {
//        return new ChartPanel(createChart(createDataset()));
//    }
//    
//    /**
//     * Starting point for the demonstration application.
//     *
//     * @param args  ignored.
//     */
//    public static void main(String[] args) {
//
//        XYBarChartDemo1 demo = new XYBarChartDemo1("State Executions - USA");
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);
//
//    }
    
    static Class class$(String s)   
    {   
        try {   
            return Class.forName(s);   
        } catch (ClassNotFoundException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
        ClassNotFoundException classnotfoundexception = null;   
        throw new NoClassDefFoundError(classnotfoundexception.getMessage());   
    }   

}